import Vue from 'vue'
import App from './App.vue'
import mint from 'mint-ui'
import 'mint-ui/lib/style.min.css'
import '../node_modules/mui/css/mui.min.css'
import '../node_modules/mui/css/icons-extra.css'
import VueRouter from 'vue-router'
// 缩略图
import VuePreview from 'vue-preview'
import Vuex from 'vuex'
Vue.use(Vuex)
Vue.use(mint)
Vue.use(VueRouter)
Vue.use(VuePreview)
import router from './router.js'
import VueResource from 'vue-resource'
Vue.use(VueResource)
let car = JSON.parse(localStorage.getItem('car') || '[]')

// vuex
let store = new Vuex.Store({
  state : {
    car : car
  },
  mutations : {
    
    addCar(state,shop) {
      var flag = false;
      state.car.some(val=>{
        if(val.id == shop.id) {
          val.count += parseInt(shop.count)
          flag = true
          return true
        }
      });
      if( flag == false) {
        state.car.push(shop)
      }
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    updataCount(state,shop) {
      state.car.some( val => {
        if(val.id == shop.id) {
          val.count = parseInt(shop.count)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    getRemove(state,id) {
      state.car.some((val,i)=>{
        if(val.id == id) {
          state.car.splice(i,1)
          return true
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    },
    selectedStatus(state,item) {
      state.car.forEach( val => {
        if(val.id == item.id) {
          val.selected = item.selected
        }
      })
      localStorage.setItem('car',JSON.stringify(state.car))
    }
  },
  getters : {
    // 商品详情里的数量 
    allCount(state) {
      var c = 0;
      state.car.forEach(val=>{
        c +=val.count
       
      })
      return c
    },
    // 购物车里的数量  直接覆盖 不加等于
    getCount(state) {
      let o = {};
      state.car.forEach( val=> {
        o[val.id] = val.count 
      })
      return o
    },
    allSelected(state) {
      let p = {};
      state.car.forEach( val=>{
        p[val.id] = val.selected
      })
      return p
    },
   countMoney(state) {
      var m = {
        count : 0,
        money : 0
      }
      state.car.forEach( val => {
        if(val.selected) {
          m.count +=  val.count;
          m.money += parseInt(val.price*val.count)
        }
      })
      return m
    } 
  }
})

Vue.http.options.root = 'http://www.liulongbin.top:3005'





// 公共时间过滤器
Vue.filter('ctime',function(data) {
  let time = new Date(data),
  y = time.getFullYear(),
  m = time.getMonth()+1,
  d = time.getDate()
  return `${y}-${m}-${d}`
})

Vue.config.productionTip = false

new Vue({
  el : '#app',
  render: c => c(App),
  router,
  store
})
